package com.the4man.cnbdw.listener.socket;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.net.*;

/**
 * Created by lenovo on 2017/12/12.
 */
public class UdpServerSocket {
    private Logger log = LoggerFactory.getLogger(UdpServerSocket.class);
    private byte[] buffer = new byte[1024];
    private DatagramSocket ds = null;
    private DatagramPacket packet = null;
    private InetSocketAddress socketAddress = null;
    private String orgIp;

    public UdpServerSocket(String host, int port) throws Exception {
        this.socketAddress = new InetSocketAddress(host, port);
        this.ds = new DatagramSocket(8889);
        this.log.info("服务端启动!");
    }

    public final String getOrgIp() {
        return this.orgIp;
    }

    public final void setSoTimeout(int timeout) throws Exception {
        this.ds.setSoTimeout(timeout);
    }

    public final int getSoTimeout() throws Exception {
        return this.ds.getSoTimeout();
    }

    public final void bind(String host, int port) throws SocketException {
        this.socketAddress = new InetSocketAddress(host, port);
        this.ds = new DatagramSocket(this.socketAddress);
        System.out.println("port" + port);
        System.out.println("host" + host);
    }

    public final String receive() throws IOException {
        this.packet = new DatagramPacket(this.buffer, this.buffer.length);
        this.ds.receive(this.packet);
        this.orgIp = this.packet.getAddress().getHostAddress();
        String info = new String(this.packet.getData(), 0, this.packet.getLength());
        this.log.info("接收信息：" + info);
        System.out.println("-----------------" + info);
        return info;
    }

    public byte[] receiveBytes() throws IOException {
        this.packet = new DatagramPacket(this.buffer, this.buffer.length);
        this.ds.receive(this.packet);
        this.orgIp = this.packet.getAddress().getHostAddress();
        return this.packet.getData();
    }

    public final void response(String info) throws IOException {
        this.log.info("客户端地址 : " + this.packet.getAddress().getHostAddress() + ",端口：" + this.packet.getPort());
        DatagramPacket dp = new DatagramPacket(this.buffer, this.buffer.length, this.packet.getAddress(), this.packet.getPort());
        dp.setData(info.getBytes());
        this.ds.send(dp);
    }

    public final void setLength(int bufsize) {
        this.packet.setLength(bufsize);
    }

    public final InetAddress getResponseAddress() {
        return this.packet.getAddress();
    }

    public final int getResponsePort() {
        return this.packet.getPort();
    }

    public final void close() {
        try {
            this.ds.close();
        } catch (Exception var2) {
            var2.printStackTrace();
        }

    }

    public static void main(String[] args) throws Exception {
        String serverHost = "127.0.0.1";
        int serverPort = 8889;
        UdpServerSocket udpServerSocket = new UdpServerSocket(serverHost, serverPort);

        while(true) {
            udpServerSocket.receive();
            udpServerSocket.response("你好,sterning!");
        }
    }
}
